package ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Color;
import java.awt.BorderLayout;
import javax.swing.border.BevelBorder;
import javax.swing.ImageIcon;

public class MyListAtt extends JPanel {
	private final static int WIDTH_LINE = 190;
	private final static int HEIGH_LINE = 20;
	/**
	 * Create the panel.
	 */
	public MyListAtt(String[] att, String[] keys) {
		setLayout(null);
		
		int len = att.length;
		
		this.setBounds(0, 0, WIDTH_LINE, HEIGH_LINE * len);
		
		for(int i = 0; i < len; i++) {
			MyLabel lb = new MyLabel(att[i], containInArray(keys, att[i]));
			lb.setBounds(0, i * HEIGH_LINE, WIDTH_LINE, HEIGH_LINE);
			this.add(lb);
		}
	}
	
	public boolean containInArray(String[] keys, String att) {
		if (keys == null) {
			return false;
		}
		
		for(String k : keys) {
			if(k.equals(att)) {
				return true;
			}
		}
		
		return false;
	}
	
	public class MyLabel extends JPanel {
		public String name;
		public MyLabel(String name, boolean isKey) {
			this.name = name;
			this.setBackground(Color.ORANGE);
			this.addMouseListener(new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent arg0) {}
				
				@Override
				public void mousePressed(MouseEvent arg0) {}
				
				@Override
				public void mouseExited(MouseEvent arg0) {
					changeBackground(Color.ORANGE);
				}
				
				@Override
				public void mouseEntered(MouseEvent arg0) {
					changeBackground(Color.CYAN);
				}
				
				@Override
				public void mouseClicked(MouseEvent arg0) {}
			});
			this.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
			this.setLayout(null);
			
			if(isKey) {
				JLabel key = new JLabel("");
				key.setIcon(new ImageIcon(MyListAtt.class.getResource("/resource/key.gif")));
				key.setBounds(0, 0, 20, 20);
				this.add(key);
			}
			
			JLabel lbName = new JLabel(this.name);
			lbName.setBounds(20, 0, 170, 20);
			this.add(lbName);
		}
		
		public void changeBackground(Color c) {
			this.setBackground(c);
		}
	}

}
